import 'package:my_flutter_core/my_flutter_core.dart';

class IncomeExpenseDetailType with ActionLogMixin {
  IncomeExpenseDetailType({
    this.id,
    this.incomeExpenseType,
    this.name,
    this.title,
    this.subtitle,
    this.analysisIncluded = true,
    DateTime? dateCreated,
    DateTime? lastUpdated,
  }) {
    this.dateCreated = dateCreated;
    this.lastUpdated = lastUpdated;
  }

  factory IncomeExpenseDetailType.fromJson(Map<String, dynamic> json) {
    return IncomeExpenseDetailType(
      id: json['id']?.toString(),
      incomeExpenseType: json['incomeExpenseType']?.toString(),
      name: json['name']?.toString(),
      title: json['title']?.toString(),
      subtitle: json['subtitle']?.toString(),
      analysisIncluded: tryToConvert<bool>(
        json['analysisIncluded'],
        (p) => p as bool,
      ),
      dateCreated: tryToConvert<DateTime>(
        json['dateCreated'],
        (p) => parseDate(p),
      ),
      lastUpdated: tryToConvert<DateTime>(
        json['lastUpdated'],
        (p) => parseDate(p),
      ),
    );
  }

  static const nameMaxLength = 50;
  static const titleMaxLength = 50;
  static const subtitleMaxLength = 50;

  String? id;
  String? incomeExpenseType;
  String? name;
  String? title;
  String? subtitle;
  bool? analysisIncluded;

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'incomeExpenseType': incomeExpenseType,
      'name': name!.toUpperCase(),
      'title': title,
      'subtitle': subtitle,
      'analysisIncluded': analysisIncluded,
    };
  }

  /// Checks if the input [detailType] has the same value.
  bool equals(IncomeExpenseDetailType detailType) {
    return id == detailType.id &&
        incomeExpenseType == detailType.incomeExpenseType &&
        name == detailType.name &&
        title == detailType.title &&
        subtitle == detailType.subtitle &&
        analysisIncluded == detailType.analysisIncluded;
  }

  IncomeExpenseDetailType clone() {
    return IncomeExpenseDetailType(
      id: id,
      incomeExpenseType: incomeExpenseType,
      name: name,
      title: title,
      subtitle: subtitle,
      analysisIncluded: analysisIncluded,
      dateCreated: dateCreated,
      lastUpdated: lastUpdated,
    );
  }
}
